# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: 7.9 将单方法的类转换为函数.py
@time: 2021/02/02
"""
from urllib.request import urlopen

class UrlTemplate:
    def __init__(self,template):
        self.template = template

    def open(self,map):
        ...
        return urlopen(self.template.format_map(map))


def test():
    ...
    # url_template = UrlTemplate('http://www.baidu.com?a={name}')
    # for line in url_template.open({'name':'tony'}):
    #     print(line.decode('utf-8'))

    # url_template = UrlTemplate('http://www.baidu.com?a={name}')
    for line in template('http://www.baidu.com?a={name}')({'name':'122'}):
        print(line.decode('utf-8'))

def template(tmp:str):
    def open(map):
        ...
        return urlopen(tmp.format_map(map))
    return open

if __name__ == '__main__':
    pass
    # print('name:{name}'.format_map({'name': 'zhangsan'}))
    test()
